﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Data;
using System.Data.Common;

namespace Sinacor.OMM.Configuration.Router
{
    /// <summary>
    /// Read router configuration
    /// </summary>
    public class RouterConfigurationReader : BaseResourceAccess
    {

        private Int32 _companyId;

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="companyId"></param>
        public RouterConfigurationReader(Int32 companyId)
            : base(companyId)
        {
            _companyId = companyId;
        }

        #endregion

        #region Methods

        
        /// <summary>
        /// Read router configuration
        /// </summary>
        /// <param name="repositoryId"></param>
        /// <returns></returns>
        public OMM.Configuration.Router.RouterConfiguration GetConfiguration()
        {
            const int defaultRetransmissionTimeout = 60000;
            //
            OMM.Configuration.Router.RouterConfiguration routerConfiguration = null;
            OMM.Configuration.Repository.RepositoryConfigurationReader repositoryReader = new OMM.Configuration.Repository.RepositoryConfigurationReader(_companyId);
            String sqlCommand = String.Empty;

            //
            sqlCommand = @" SELECT COD_REPRIO_MSG_ROTM,
                                   COD_REPRIO_MSG_COMU_ROTR,
                                   TEM_REPE_TMSS
                           FROM TBOCNFG_ROTR ";

            using (DbCommand dbCommand = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                routerConfiguration = this.FindOne<RouterConfiguration>(dbCommand,
                    reader =>
                    {
                        RouterConfiguration item = new RouterConfiguration();

                        item.MessageMainRepository.Id = reader.GetValue<Int32>("COD_REPRIO_MSG_ROTM");
                        item.SystemMessageRepository.Id = reader.GetValue<Int32>("COD_REPRIO_MSG_COMU_ROTR");
                        item.RetransmissionTimeout = reader.GetValue<Int32?>("TEM_REPE_TMSS");

                        return item;

                    });
            }

            if (routerConfiguration != null)
            {
                //Message main repository
                if (routerConfiguration.MessageMainRepository.Id.HasValue)
                {
                    routerConfiguration.MessageMainRepository = repositoryReader.GetConfiguration(routerConfiguration.MessageMainRepository.Id.Value);
                }
                else
                {
                    routerConfiguration.MessageMainRepository = null;
                }

                //System Message Repository
                if (routerConfiguration.SystemMessageRepository.Id.HasValue)
                {
                    routerConfiguration.SystemMessageRepository = repositoryReader.GetConfiguration(routerConfiguration.SystemMessageRepository.Id.Value);
                }
                else
                {
                    routerConfiguration.SystemMessageRepository = null;
                }

                if (!routerConfiguration.RetransmissionTimeout.HasValue || (routerConfiguration.RetransmissionTimeout.Value == 0))
                    routerConfiguration.RetransmissionTimeout = defaultRetransmissionTimeout;

                routerConfiguration.DistributionRules = this.GetDistributionRules();
            }
            
            return routerConfiguration;
        }

        /// <summary>
        /// Read the distribution rules
        /// </summary>
        /// <returns></returns>
        private List<DistributionRule> GetDistributionRules()
        {
            //
            String sqlCommand = String.Empty;
            List<DistributionRule> distributionRules = null;
            HFTMessageManager.HFTMessageManagerConfigurationReader hftReader = new HFTMessageManager.HFTMessageManagerConfigurationReader(_companyId);

            //
            sqlCommand = @" SELECT CD_CLIENTE,
                                   NUM_PRTA_INIC,
                                   NUM_PRTA_FIM,
                                   COD_PROS_IAFR
                            FROM TBOCNFG_DIST_MSG_IAFR ";

            using (DbCommand dbCommand = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                distributionRules = this.FindAll<DistributionRule>(dbCommand,
                    reader =>
                    {
                        DistributionRule item = new DistributionRule();

                        item.StartPort = reader.GetValue<Int32>("NUM_PRTA_INIC");
                        item.EndPort = reader.GetValue<Int32>("NUM_PRTA_FIM");
                        item.ClientId = reader.GetValue<Int32?>("CD_CLIENTE");
                        item.HFTProcess.Id = reader.GetValue<Int32>("COD_PROS_IAFR");

                        return item;
                    });
            }

            foreach (DistributionRule item in distributionRules)
                item.HFTProcess = hftReader.GetConfiguration(item.HFTProcess.Id);   
            
            return distributionRules;
        }

        #endregion

    }
}
